package com.heather.exam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heather.exam.mapper.QuestionMapper;
import com.heather.exam.pojo.table.Options;
import com.heather.exam.pojo.table.Question;
import com.heather.exam.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {
    @Autowired
    QuestionMapper questionMapper;
    @Override
    @Transactional
    public boolean addQuestion(Question question, List<Options> options,String bankid) {
        boolean result = save(question);
        if (result) {
            boolean optionsResult = true;
            for (Options option : options) {
                option.setQuestionId(question.getId());
                boolean result1 = questionMapper.AddOptions(option);
                if (!result1) {
                    optionsResult = false;
                    result = optionsResult;
                    return result;
                }
            }
            int id = question.getId();
            boolean b = questionMapper.AddBank(id, bankid);
            questionMapper.addShare(id);
            if (!b) {
                result = false;
            }
            return result;
        }
        return false;
    }

    @Override
    public int check(String questionBankid) {
        return questionMapper.check(questionBankid);
    }

    @Override
    public void addShare(String questionBankid, String state) {
        questionMapper.addShare(Integer.parseInt(questionBankid));
    }

    @Override
    public void updateShare(String questionBankid, String state) {
        questionMapper.UpdateShare(Integer.parseInt(questionBankid),Integer.parseInt(state));
    }


}
